package com.cab.dao;

import com.cab.entity.Cab;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Repository;

import java.util.List;
@Repository
@Scope("prototype")
public class CabDaoImpl implements CabDao {
    @Autowired
    private SessionFactory sessionFactory = null;

    public boolean add(Cab cab) {
        boolean flag = false;
        Session session = sessionFactory.getCurrentSession();
        session.save(cab);
        session.getTransaction().commit();
        flag = true;
        return flag;
    }

    public boolean update(Cab cab) {
        boolean flag = false;
        Session session = sessionFactory.getCurrentSession();
        Cab cab1 = session.get(Cab.class,cab.getId());
        cab1.setId(cab.getId());
        cab1.setAddr(cab.getAddr());
        session.update(cab1);
        flag = true;
        session.getTransaction().commit();
        return flag;
    }

    public boolean delete(Cab cab) {
        boolean flag = false;
        Session session = sessionFactory.getCurrentSession();
        Cab cab1 = session.get(Cab.class,cab.getId());
        session.delete(cab1);
        flag = true;
        session.getTransaction().commit();
        return flag;
    }

    public List<Cab> select(Cab cab) {
        List<Cab> cabList = null;
        Session session  = sessionFactory.getCurrentSession();
        org.hibernate.query.Query query = session.createQuery("from Cab");
        cabList = query.getResultList();
        return cabList;
    }

    public Cab selectOne(Cab cab) {
        List<Cab> cabList = null;
        Session session  = sessionFactory.getCurrentSession();
        org.hibernate.query.Query query = session.createQuery("from Cab u where u.id=ee");
        query.setParameter(0,cab.getId());
        cabList = query.getResultList();
        Cab cab1 = cabList.get(0);
        return cab1;
    }
}
